/**    
 * @Title: MyTitleView.java  
 * @Package com.anguanjia.safe.ui_rebuild  
 * @Description: TODO  
 * @author wuxu    
 * @date 2012-6-25 下午01:52:24  
 * @version V1.0    
 */
package com.ksl.kinglockguard.widget.common;

import java.util.HashMap;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.ksl.kinglockguard.R;


public class MyTitleView extends LinearLayout {

	LinearLayout second_title_view;
	TextView second_title_text1;
	TextView second_title_text2;
	TextView newsText;
	TextView leftTitle;
	MarqueeTextView title_text;
	LinearLayout menu_view;
	RelativeLayout lay_title_back;
	LinearLayout recomendNews;
	ImageView back_img;
	MyTitleView myTitleView;
	LinearLayout text_checkbox_ll;
	LinearLayout titleBackView;
	Activity context;
	private View view;
	private MenuImageResourceListener imageResourceListener;
	private boolean isParentPage;

	public MyTitleView(final Activity activity) {
		super(activity);
		context = activity;
		myTitleView = (MyTitleView) activity.findViewById(R.id.ur_title);
		setOrientation(LinearLayout.HORIZONTAL);
		LayoutInflater mInflater = LayoutInflater.from(activity);
		view = mInflater.inflate(R.layout.lock_ur_title_view, null);
		title_text = (MarqueeTextView) view.findViewById(R.id.title_text_view);
		title_text.requestFocus();
		newsText = (TextView)view.findViewById(R.id.newsText);
		leftTitle = (TextView)view.findViewById(R.id.left_title);
		menu_view = (LinearLayout) view.findViewById(R.id.menu_view);
		text_checkbox_ll = (LinearLayout) view.findViewById(R.id.text_checkbox_ll);
		recomendNews = (LinearLayout) view.findViewById(R.id.recomendNews);
		lay_title_back = (RelativeLayout) view.findViewById(R.id.lay_title_back);
		back_img = (ImageView) view.findViewById(R.id.title_img_icon);
		titleBackView = (LinearLayout) view.findViewById(R.id.title_back_view);
		//不能设置lay_title_back的点击事件，否则点击标题也会finish当前Activity   //by  wmmeng
		lay_title_back.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
                if( imageResourceListener != null) {
                    imageResourceListener.goBackPreviousPage(!isParentPage);
                }
			}
		});
//		back_img.setOnClickListener(new View.OnClickListener() {
//
//			@Override
//			public void onClick(View v) {
//				imageResourceListener.goBackPreviousPage(!isParentPage);
////				if (!isParentPage) {
////					KingsSystemUtils.hideKeyboard(v);
////					activity.finish();
////				}
//			}
//		});

		myTitleView.addView(view, new LayoutParams(LayoutParams.FILL_PARENT,
				LayoutParams.WRAP_CONTENT));
	}

	public MyTitleView(final Activity activity, boolean isParentPage) {
		this(activity);
		this.isParentPage = isParentPage;
		if (isParentPage) {
			lay_title_back.setClickable(false);
		} else {			
			lay_title_back.setClickable(true);
			recomendNews.setVisibility(INVISIBLE);
			lay_title_back.setVisibility(VISIBLE);
		}
	}
	
	public void setTitleViewVisibility(int status) {
		myTitleView.setVisibility(status);
	}

	public void setTitleGravity(int gravity){
		title_text.setGravity(gravity);
	}
	public int getLayoutHeight() {

		return myTitleView.getHeight();
	}

	public MyTitleView(final Context context, AttributeSet attrs) {
		super(context, attrs);
	}

	public void setTitle(String title) {
		if (title != null) {
			title_text.setText(title);
		}
	}
	
	public void setLaybackTitleVisibility(boolean isVisible) {
		if (isVisible) {
			title_text.setVisibility(View.VISIBLE);
		}else{
			title_text.setVisibility(View.INVISIBLE);
		}
	}
	
	public void setCenterTitleVisibility(boolean isVisible){
		if (isVisible) {
			recomendNews.setVisibility(View.VISIBLE);
		}else{
			recomendNews.setVisibility(View.INVISIBLE);
		}
	}
	
	public void setCenterTitleText(String title){
		newsText.setText(title);
	}
	
	
	public void setTitleColor(int color) {
			title_text.setTextColor(color);
	}

	public void setTitleBackGround(int res) {
		if (lay_title_back != null) {
			lay_title_back.setBackgroundResource(res);
		}
	}

	public void setTitle(int title) {
		if (title > 0) {
			title_text.setText(title);
		}
	}

	public void setBackClicklintener(View.OnClickListener listener) {
		//不能设置lay_title_back的点击事件，否则点击标题也会finish当前Activity   //by  wmmeng
//		lay_title_back.setOnClickListener(listener);
		back_img.setOnClickListener(listener);
	}
	
	public void setImgBtnUnclickable(int _id) {
		view_map.get(_id).setClickable(false);
	}
	
	public void setImgBtnClickable(int _id) {
		view_map.get(_id).setClickable(true);
	}

	HashMap<Integer, View> view_map;

	public void addMenuView(int _id, int resId, OnClickListener listener) {
		if (view_map == null) {
			view_map = new HashMap<Integer, View>();
		}
		if (view_map.containsKey(_id)) {
			menu_view.removeView(view_map.get(_id));
			view_map.remove(_id);
		}
		ImageView menu_img = new ImageView(context);
		if (menu_img != null) {
			menu_img.setImageResource(resId);
			// if (resId != R.drawable.title_share)
			//menu_img.setBackgroundResource(R.drawable.title_img_click_default);
            if(imageResourceListener != null) {
                imageResourceListener.setMenuImageResource(menu_img,_id);
            }
			menu_img.setOnClickListener(listener);
			DisplayMetrics dm = new DisplayMetrics();
			context.getWindowManager().getDefaultDisplay().getMetrics(dm);
//			if (dm.widthPixels <= 240) {
//				menu_img.setPadding(5, 0, 5, 0);
//			} else if (dm.widthPixels <= 320) {
//				menu_img.setPadding(8, 0, 8, 0);
//			} else if (dm.widthPixels <= 480) {
//				menu_img.setPadding(12, 0, 12, 0);
//			} else if (dm.widthPixels <= 550) {
//				menu_img.setPadding(12, 0, 12, 0);
//			} else if (dm.widthPixels <= 640) {
//				menu_img.setPadding(12, 0, 12, 0);
//			} else {
//				menu_img.setPadding(20, 0, 20, 0);
//			}
			android.widget.RelativeLayout.LayoutParams params = new android.widget.RelativeLayout.LayoutParams(
					(int)getResources().getDimension(R.dimen.lock_iv_title_menu_width),(int) getResources().getDimension(R.dimen.lock_iv_title_menu_height));
			params.leftMargin = 20;
			menu_view.addView(menu_img, 0, params);
			view_map.put(_id, menu_img);
		}

	}
	
	public void addMenuView(int _id, Bitmap drawable, OnClickListener listener) {
		if (view_map == null) {
			view_map = new HashMap<Integer, View>();
		}
		if (view_map.containsKey(_id)) {
			menu_view.removeView(view_map.get(_id));
			view_map.remove(_id);
		}
		ImageView menu_img = new ImageView(context);
		if (menu_img != null) {
		//	menu_img.setBackgroundResource(R.drawable.title_img_click_default2);
			menu_img.setImageBitmap(drawable);
			menu_img.setOnClickListener(listener);
			DisplayMetrics dm = new DisplayMetrics();
			context.getWindowManager().getDefaultDisplay().getMetrics(dm);
			if (dm.widthPixels <= 240) {
				menu_img.setPadding(5, 0, 5, 0);
			} else if (dm.widthPixels <= 320) {
				menu_img.setPadding(8, 0, 8, 0);
			} else if (dm.widthPixels <= 480) {
				menu_img.setPadding(12, 0, 12, 0);
			} else if (dm.widthPixels <= 550) {
				menu_img.setPadding(12, 0, 12, 0);
			} else if (dm.widthPixels <= 640) {
				menu_img.setPadding(12, 0, 12, 0);
			} else {
				menu_img.setPadding(20, 0, 20, 0);
			}
			
			menu_view.addView(menu_img, 0, new LayoutParams(
					LayoutParams.WRAP_CONTENT, LayoutParams.FILL_PARENT));
			view_map.put(_id, menu_img);
		}
		
	}

	public void addMenuView(int _id, int resId, View view,
			OnClickListener listener) {/*
		if (view_map == null) {
			view_map = new HashMap<Integer, View>();
		}
		if (view_map.containsKey(_id)) {
			menu_view.removeView(view_map.get(_id));
			view_map.remove(_id);
		}
		view.setBackgroundResource(R.drawable.title_img_click_default);
		view.setOnClickListener(listener);
		ImageView menu_img = (ImageView) view.findViewById(R.id.img_menu);
		if (menu_img != null) {
			menu_img.setImageResource(resId);
			DisplayMetrics dm = new DisplayMetrics();
			context.getWindowManager().getDefaultDisplay().getMetrics(dm);
			if (dm.widthPixels <= 240) {
				menu_img.setPadding(5, 0, 5, 0);
			} else if (dm.widthPixels <= 320) {
				menu_img.setPadding(8, 0, 8, 0);
			} else if (dm.widthPixels <= 480) {
				menu_img.setPadding(12, 0, 12, 0);
			} else if (dm.widthPixels <= 550) {
				menu_img.setPadding(12, 0, 12, 0);
			} else if (dm.widthPixels <= 640) {
				menu_img.setPadding(12, 0, 12, 0);
			} else {
				menu_img.setPadding(20, 0, 20, 0);
			}

			menu_view.addView(view, 0, new LayoutParams(
					LayoutParams.WRAP_CONTENT, LayoutParams.FILL_PARENT));
			view_map.put(_id, menu_img);
		}

	*/}

	public void removeMenuView(int id) {
		if (view_map != null && view_map.containsKey(id)) {
			View menu_img = view_map.get(id);
			if (menu_img != null) {
				menu_view.removeView(menu_img);
			}
			view_map.remove(id);

		}
	}

	public void setMenuViewStatue(int id, int statue) {
		if (view_map != null && view_map.containsKey(id)) {
			view_map.get(id).setVisibility(statue);
		}
	}

	public View getMenuView(int id) {
		if (view_map != null && view_map.containsKey(id)) {
			return view_map.get(id);
		}
		return null;
	}

	public void setMenuVisibility(int visibility) {
		menu_view.setVisibility(visibility);
	}
	
	public void setMenuViewVisibility(int visibility){
		text_checkbox_ll.setVisibility(visibility);
	}

	public void setMenuBackground(int id, int background) {
		if (background > 0) {
			if (view_map != null) {
				View view = view_map.get(id);
				if (view != null && view instanceof ImageView) {
					((ImageView) view).setImageResource(background);
				}
			}
		}
	}

	public void setMenuBackground(int id, Drawable drawable) {
		if (drawable != null) {
			if (view_map != null) {
				View view = view_map.get(id);
				if (view != null && view instanceof ImageView) {
					((ImageView) view).setImageDrawable(drawable);
				}
			}
		}
	}

	public void setMenuBackground(int id, Bitmap bitmap) {
		if (bitmap != null) {
			if (view_map != null) {
				View view = view_map.get(id);
				if (view != null && view instanceof ImageView) {
					((ImageView) view).setImageBitmap(bitmap);
				}
			}
		}
	}

	public void addMenuButton(final String text,
			final View.OnClickListener listener) {
		Button button = new Button(context);
		button.setText(text);
		button.setOnClickListener(listener);
		menu_view.addView(button, new LayoutParams(LayoutParams.WRAP_CONTENT,
				LayoutParams.FILL_PARENT));
	}

	public void setSecondTitleVisibility(int visibility) {
		second_title_view.setVisibility(View.GONE);
	}

	public void setSecondText1(int text) {
		if (text > 0) {
			second_title_text1.setText(text);
		}
	}

	public void setSecondText1(String text) {
		if (text != null) {
			second_title_text1.setText(text);
		}
	}

	public void setSecondText2Visibility(int visibility) {
		second_title_text2.setText(visibility);
	}

	public void setSecondText2(int text) {
		if (text > 0) {
			second_title_text2.setText(text);
		}
	}

	public void setSecondText2(String text) {
		if (text != null) {
			second_title_text2.setText(text);
		}
	}
	
	public void setTitleBarBackground(int color){
		view.setBackgroundColor(color);
	}
	public void setImgBtnGray(int id) {
		imageResourceListener.setImageBtnGray((ImageView)view_map.get(id), id);
	}
	
	public void setImgBtnNormal(int id) {
		imageResourceListener.setImageBtnNormal((ImageView)view_map.get(id), id);
	}

	public void setMenuImageResourceListener(MenuImageResourceListener imageResourceListener) {
		this.imageResourceListener = imageResourceListener;
	}
	
//	public void goBackPage() {
//		imageResourceListener.goBackPreviousPage();
//	}
	
	public interface MenuImageResourceListener {
		public void setMenuImageResource(ImageView menu_img, int id);
		public void setImageBtnGray(ImageView menu_img, int id);
		public void setImageBtnNormal(ImageView menu_img, int id);
		public void goBackPreviousPage(boolean parentPage);
	}
}
